package com.tanhua.server.service;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.conn.FdfsWebServer;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.tanhua.common.enums.SexEnum;
import com.tanhua.common.pojo.User;
import com.tanhua.common.pojo.UserInfo;
import com.tanhua.common.service.PicUploadService;
import com.tanhua.common.utils.UserThreadLocal;
import com.tanhua.common.vo.PicUploadResult;
import com.tanhua.dubbo.server.api.TaoHuaApi;
import com.tanhua.dubbo.server.pojo.Sound;
import com.tanhua.dubbo.server.pojo.Video;
import com.tanhua.dubbo.server.vo.TaoHuavo;
import com.tanhua.server.vo.TaoHuacyvo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;


@Service
@Slf4j
public class TaoHuaService {


    @Autowired
    protected FastFileStorageClient storageClient;

    @Autowired
    private FdfsWebServer fdfsWebServer;


    @Autowired
    protected UserInfoService userInfoService;

    @Reference(version = "1.0.0")
    private TaoHuaApi taoHuaApi;

    public Boolean saveSound(MultipartFile soundFile) {
        //获取用户信息
        User user = UserThreadLocal.get();
        // 创建视频表对象
        Sound sound = new Sound();
        sound.setUserId(user.getId());


        try {

            //上传视频(流对象,文件大小,文件后缀名(去文件名最后的.作为分割点))
            StorePath storePath = storageClient.uploadFile(soundFile.getInputStream(),
                    soundFile.getSize(),
                    StrUtil.subAfter(soundFile.getOriginalFilename(), '.', true),
                    null);

            //设置视频url
            sound.setSouUrl(fdfsWebServer.getWebServerUrl() + storePath.getFullPath());
            //将对象传递给dubbo层 存放到MongoDB中
            Boolean result = this.taoHuaApi.saveSound(sound);
            return result;
        } catch (Exception e) {
            log.error("发布语音失败！file = ", e);
        }

        return false;
    }

    public TaoHuacyvo getSound() {
        User user = UserThreadLocal.get();

        TaoHuacyvo taoHuacyvo = new TaoHuacyvo();

        //2.查询MongoDB中的数据
        TaoHuavo sound = this.taoHuaApi.getSound(user.getId());

        // 2.2 判断数据的存在性
        if (ObjectUtil.isEmpty(sound)) {
            return taoHuacyvo;
        }

        taoHuacyvo.setSoundUrl(sound.getSoundUrl());
        taoHuacyvo.setRemainingTimes(sound.getRemainingTimes());


        UserInfo userInfo1 = this.userInfoService.queryUserInfoByUserId(sound.getUserid());


        taoHuacyvo.setId(Convert.toInt(userInfo1.getUserId()));
        taoHuacyvo.setAvatar(userInfo1.getLogo());
        taoHuacyvo.setNickname(userInfo1.getNickName());
        taoHuacyvo.setAge(userInfo1.getAge());
        taoHuacyvo.setGender(userInfo1.getSex() == SexEnum.MAN? "man" : "woman");


        return taoHuacyvo;
    }
}
